package com.adb.receivecontent.uc;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.adb.receivecontent.R;
import com.adb.receivecontent.bean.LinkMagnetBean;

public class LinkMagnetAdapter extends BaseAdapter{
	
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	public static LinkedList<LinkMagnetBean> mLinkListLinkMagnet = new LinkedList<LinkMagnetBean>();
	private IMenuClickListener mClickCallback;
	
	public LinkMagnetAdapter(Context mContext){
		this.mContext = mContext;
		this.mLayoutInflater = LayoutInflater.from(this.mContext);
		mClickCallback = (IMenuClickListener)mContext;
	}
	
	public synchronized void addLinkItems(LinkMagnetBean mTempBean){
		mLinkListLinkMagnet.add(mTempBean);
		notifyDataSetChanged();
	}
	
	public synchronized void addLinksItems(ArrayList<LinkMagnetBean> mArrayList){
		
		
		if(!mLinkListLinkMagnet.isEmpty()){
			mLinkListLinkMagnet.clear();
		}
		
		for(LinkMagnetBean mTempBean : mArrayList){
			mLinkListLinkMagnet.add(mTempBean);
		}
		notifyDataSetChanged();
	
	}
	
	public synchronized void removeAddedLinkedItems(int position){
		
		if(mLinkListLinkMagnet != null){
			mLinkListLinkMagnet.remove(position);
		}
		notifyDataSetChanged();
	}
	
	
	@Override
	public int getCount() {
		return mLinkListLinkMagnet.size();
	}

	@Override
	public Object getItem(int position) {
		return mLinkListLinkMagnet.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		CatchViewHolder mCatchView;
		
		if(convertView == null){
			
			convertView = mLayoutInflater.inflate(R.layout.row_created_link_list,parent,false);
			mCatchView = new CatchViewHolder();
			mCatchView.mTextView_Name_Link = (TextView)convertView.findViewById(R.id.textView_link_name);
			
			mCatchView.mImageView_Edit = (ImageView)convertView.findViewById(R.id.imageview_edit_link);
			
			mCatchView.mImageView_Delete = (ImageView)convertView.findViewById(R.id.imageview_delete_link);
			
			convertView.setTag(mCatchView);
			
			
		}else{
			
			mCatchView = (CatchViewHolder)convertView.getTag();
		}
		
		LinkMagnetBean mLinkMagBean;
		if (mLinkListLinkMagnet != null) {
			
			mLinkMagBean = mLinkListLinkMagnet.get(position);
			
			mCatchView.mTextView_Name_Link.setText(mLinkMagBean.getStrlinkName());
			
			mCatchView.mImageView_Edit.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mClickCallback.onLinkMenuClick(R.id.imageview_edit_link,position);
				}
			});
			
			mCatchView.mImageView_Delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mClickCallback.onLinkMenuClick(R.id.imageview_delete_link,position);
				}
			});
			
		}
		
		return convertView;
	}
	
	class CatchViewHolder
	{
		TextView mTextView_Name_Link;
		ImageView mImageView_Edit;
		ImageView mImageView_Delete;
		
	}
	
}
